var win;
var form;
$(function(){
	//学生列表graid
	win = $('#stu-window').window({closed:true});
	form = win.find('form');
	var grid=$('#stu-table').datagrid({
		title:'学生基本信息',
		iconCls:'icon-save',		
		url:'mentor/student.do?action=list',
		pagination:true,	//显示分页
		rownumbers:true, 	//显示行号
		fit:true,
		fitColumns: true, 	//自动调整各列，用了这个属性，下面各列的宽度值就只是一个比例。
		collapsible:true,	//可折叠
		singleSelect:false,	//允许多选
		idField:'id', 	//主键字段,easyui的datagrid有个bug,必须把主键单独列出,才能多选  
		columns:[[
			{field:'ck',checkbox:true,width:2}, //显示复选框
			{field:'id',title:'id',width:15,align:'center',hidden:true},
			{field:'number',title:'学号',width:15,align:'center'},
			{field:'card',title:'身份证号',width:20,align:'center'},
			{field:'name',title:'姓名',width:20,align:'center'},
			{field:'sex',title:'性别',width:5,align:'center',
				formatter:function(value,rowData,rowIndex){
					if(value==true){
						return '男';
					} else if (value==false){
						return '女';
					} else {
						return '';
					}
				}	
			},
			{field:'telephone',title:'电话',width:15,align:'center'},
			{field:'addr',title:'家庭地址',width:15,align:'center'},
			{field:'room',title:'寝室',width:15,align:'center'},
			{field:'job',title:'担任职务',width:15,align:'center'},
			{field:'isclassleader',title:'班委',width:15,align:'center',
				formatter:function(value,rowData,rowIndex){
					if(value==true){
						return '是';
					} else if (value==false){
						return '不是';
					} else {
						return '';
					}
				}	
			}
			
		]],
		toolbar:'#toolbar'
	});
	
	//为保存按钮添加事件
	$('#btn-save').click(function() {
		form.form('submit', {
			url:form.url,
			success:function(data){
				eval('data='+data);
				if (data.success){
					grid.datagrid('reload');
					win.window('close');
				} else {
					$.messager.alert('错误',data.msg,'error');
				}
			}
		});	
	});
	
	//为取消按钮添加事件
	$('#btn-cancel').click(function() {
			win.window('close');
	});
	
	//新增
	$('#btn-add').click(function() {
	    form.form('clear');
        $('#classid').combobox({
            url:'class.do?action=selectByTeacherid',
            valueField:'id',
            textField:'name'
        });             
        win.window('open');
        form.url = "mentor/student.do?action=add";
	});
	//修改
    $('#btn-edit').click(function() {
        //取得选中的所有行
        var rows = $('#stu-table').datagrid('getSelections');
        if(rows.length==0){
            $.messager.show({
                title:'提示', 
                msg:'请先选择要修改的学生。'
            });
            return;
        } else if(rows.length > 1){
            $.messager.show({
                title:'提示', 
                msg:'只能选择一位学生进行修改。'
            });     
            return;
        }
        $('#classid').combobox({
            url:'class.do?action=selectByTeacherid',
            valueField:'id',
            textField:'name'
        });
        //从后台将学生的信息加载到表单
        form.form('load', 'mentor/student.do?action=getByid&id='+rows[0].id);
        win.window('open');
        form.url = 'mentor/student.do?action=edit&id='+rows[0].id;
        //清空之前的选择
        $('#stu-table').datagrid('clearSelections');
    });
    //删除
    $('#btn-del').click(function() {
        //取得选中的所有行
        var rows = $('#stu-table').datagrid('getSelections');
        if(rows.length==0){
            $.messager.show({
                title:'提示', 
                msg:'请先选择要删除的学生。'
            });
            return;
        }
        var ids = [];
        for (var i=0; i < rows.length; i++) {
            ids.push(rows[i].id);
        };
        $.getJSON('mentor/student.do?action=del',{ids:ids.join()},function(data){
            if(data.success){                        
                grid.datagrid('reload');
            }
            $.messager.show({
                title:'提示', 
                msg:data.msg
            });
        });
    });
    //密码重置
     $('#btn-reset').click(function() {
        //取得选中的所有行
        var rows = $('#stu-table').datagrid('getSelections');
        if(rows.length==0){
            $.messager.show({
                title:'提示', 
                msg:'请先选择要重置密码的学生。'
            });
            return;
        }
        var ids = [];
        for (var i=0; i < rows.length; i++) {
            ids.push(rows[i].id);
        };
        $.getJSON('mentor/student.do?action=rest',{ids:ids.join()},function(data){
            if(data.success){                        
                grid.datagrid('reload');
            }
            $.messager.show({
                title:'提示', 
                msg:data.msg
            });
        });
    });
    
    //查找   
    $('#btn-search').click(function() {
        $('#stu-table').datagrid({
            queryParams:{
                queryName:$('#queryName').val()
            }
        });
    });
});
